import javax.swing.tree.TreeNode;
import java.util.Stack;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;

    /**
     * 创建一棵二叉树 创建成功后 返回根节点
     * @return
     */
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
//中序
   void inOrder(TreeNode root){
        if (root==null){
            return;
        }
        inOrder(root.left);
       System.out.print(root.val+" ");
       inOrder(root.right);
   }
   //后序
   void postOdred(TreeNode root){
        if (root==null){
            return;
        }
        postOdred(root.left);
        postOdred(root.right);
       System.out.print(root.val+" ");
   }
   //节点的个数
   public int nodesize;
    int size(TreeNode root){
        if (root==null){
            return 0;
        }
        size(root.left);
        size(root.right);
        nodesize++;

        return nodesize;
    }
    //叶子的个数
public int leafsize;
    int getLeafnode(TreeNode root){
        if (root==null){
            return 0;
        }
        if (root.left==null&&root.right==null){
            leafsize++;
        }
        getLeafnode(root.left);
        getLeafnode(root.right);
        return leafsize;

    }
    //获取第k层节点的个数
    int getLeveNodecount(TreeNode root,int k){
        if (root==null){
            return 0;
        }
        if (k==1){
            return 1;
        }
        return getLeveNodecount(root.left,k-1)+
                getLeveNodecount(root.right,k-1);
    }
 //获取二叉树的高度
    int getHeight(TreeNode root){
        if (root==null){
            return 0;

        }
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);
        return leftHeight>rightHeight?leftHeight+1:rightHeight+1;

    }
//检查val元素是否存在
    TreeNode find(TreeNode rood,int val){
        if (rood==null){
            return null;
        }
        if (rood.val==val){
            return rood;
        }
        TreeNode ret1=find(rood.left,val);
        if (ret1!=null){
            return ret1;
        }
        TreeNode ret2=find(rood.right,val);
        if (ret2!=null){
            return ret2;
        }
        return null;
    }
    //判断两课树是不是相同
    public boolean isSameTree(TreeNode p,TreeNode q){
        if (p==null&&q==null){
            return false;
        }
        if (p==null&&q==null){
            return true;
        }
        if (p.val!=q.val){
            return false;
        }
        return isSubtree(p.left,q.right)&&isSubtree(p.right,q.left);
    }
    //判断二叉树是不是另一课树的子树
    public boolean isSubtree(TreeNode root,TreeNode subroot ){
        if (root==null||subroot==null){
            return false;
        }
        if (isSameTree(root,subroot)){
            return true;
        }
        if (isSubtree(root.left,subroot)){
            return true;
        }
        if (isSubtree(root.right,subroot)){
            return true;
        }
        return false;
    }



}
